<?php

class Message
{
	protected $sn;
	protected $pwd;
	protected $sign;
	
	public function __construct()
	{
		$CI =& get_instance();
		$CI->load->config('message');
		$message = $CI->config->item('message');
		$this->sn = $message['sn'];
		$this->pwd = $message['pwd'];
		$this->sign = $message['sign'];
	}
	
	//发送短信
	public function send_message($phone, $content,$type)
	{
		header("Content-Type: text/html; charset=UTF-8");
		$flag = 0;
		$params='';
		if($type == 1) {
			$content = '您正在注册成为蜜蜂帮忙用户，验证码:'.$content.'，请勿将验证码告知他人。';
		}
		//要post的数据
		$argv = array(
				'sn'=> $this->sn,//'SDK-SYN-010-00108', ////替换成您自己的序列号
				'pwd'=>strtoupper(md5($this->sn.$this->pwd)), //此处密码需要加密 加密方式为 md5(sn+password) 32位大写
				'mobile'=> $phone,//'15557198112',//手机号 多个用英文的逗号隔开 post理论没有长度限制.推荐群发一次小于等于10000个手机号
				'content'=> $content.'【'.$this->sign.'】',//iconv( "GB2312", "gb2312//IGNORE" ,'您好测试短信[XXX公司]'),//'您好测试,短信测试[签名]',//短信内容
				'ext'=>'',
				'stime'=>'',//定时时间 格式为2011-6-29 11:09:21
				'msgfmt'=>'',
				'rrid'=>''
		);
		//构造要post的字符串
		//echo $argv['content'];
		foreach ($argv as $key=>$value) {
			if ($flag!=0) {
				$params .= "&";
				$flag = 1;
			}
			$params.= $key."="; $params.= urlencode($value);// urlencode($value);
			$flag = 1;
		}
		$length = strlen($params);
		//创建socket连接
		$fp = fsockopen("sdk.entinfo.cn",8061,$errno,$errstr,10) or exit($errstr."--->".$errno);
		//构造post请求的头
		$header = "POST /webservice.asmx/mdsmssend HTTP/1.1\r\n";
		$header .= "Host:sdk.entinfo.cn\r\n";
		$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
		$header .= "Content-Length: ".$length."\r\n";
		$header .= "Connection: Close\r\n\r\n";
		//添加post的字符串
		$header .= $params."\r\n";
		//发送post的数据
		//echo $header;
		//exit;
		fputs($fp,$header);
		$inheader = 1;
		while (!feof($fp)) {
			$line = fgets($fp,1024); //去除请求包的头只显示页面的返回数据
			if ($inheader && ($line == "\n" || $line == "\r\n")) {
				$inheader = 0;
			}
			if ($inheader == 0) {
				// echo $line;
			}
		}
		//<string xmlns="http://tempuri.org/">-5</string>
		$line=str_replace("<string xmlns=\"http://tempuri.org/\">","",$line);
		$line=str_replace("</string>","",$line);
		$result=explode("-",$line);
		// echo $line."-------------";
		if(count($result)>1)
			//echo '发送失败返回值为:'.$line.'。请查看webservice返回值对照表';
			return false;
		else
			//echo '发送成功 返回值为:'.$line;
			return true;
	}
	
	//验证短信
	public function register()
	{

	}
} 